package com.cvte.boe.media.views.settingpage;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cvte.boe.media.R;
import com.cvte.boe.media.dao.MediaSettingDao;
import com.cvte.boe.media.dao.MediaSettingDaoImpl;
import com.cvte.boe.media.utils.LogUtil;
import com.cvte.boe.media.utils.SettingUtil;
import com.cvte.boe.media.views.widget.ComboView;

/**
 * Created by wuguitong on 2018/5/3.
 */
public class PageImagePlayerSettings extends LinearLayout implements ComboView.ComboViewUpdateListener,View.OnClickListener{
    private ComboView comboView;
    private Context mContext;
    private MediaSettingDao dao;
    private OnImageSettingListener listener = null;
    private Button btnLoopOn;
    private Button btnLoopOff;
    private TextView tvTimer;

    public interface OnImageSettingListener {
        void onUpdateImageLoop(SettingUtil.ILOOP loop);
        void onUpdateImageLoopTimer(SettingUtil.ILOOPTIMER timer);
    }
    public PageImagePlayerSettings(Context context) {
        this(context,null);
    }

    public PageImagePlayerSettings(Context context, AttributeSet attrs) {
        this(context, null,0);
    }

    public PageImagePlayerSettings(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        dao = new MediaSettingDaoImpl(mContext);
        inflate(mContext, R.layout.page_image_player_setting,this);
        comboView = (ComboView)findViewById(R.id.cv_image_loop_timer);
        comboView.initList(SettingUtil.getList(mContext,SettingUtil.IMAGELOOPTIMER));
        comboView.setCurrentIndex(dao.getImageLoopTimer().ordinal());
        comboView.setComboViewListener(this);
        btnLoopOn = (Button)findViewById(R.id.btn_image_loop_on);
        btnLoopOff = (Button)findViewById(R.id.btn_image_loop_off);
        btnLoopOff.setOnClickListener(this);
        btnLoopOn.setOnClickListener(this);
        updateLoopSetting(dao.getImageLoop());
        tvTimer = (TextView)findViewById(R.id.tv_image_loop_timer);
        updateTimerText(dao.getImageLoopTimer());
    }
    @Override
    public boolean onUpdateListener(ComboView view, int index) {
        dao.setImageLoopTimer(SettingUtil.ILOOPTIMER.values()[index]);
        updateTimerText(SettingUtil.ILOOPTIMER.values()[index]);
        if(listener != null){
            listener.onUpdateImageLoopTimer(SettingUtil.ILOOPTIMER.values()[index]);
        }
        return true;
    }
    public void setOnImagePlayerListener(OnImageSettingListener listener){
        this.listener = listener;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_image_loop_on:
                updateLoopSetting(SettingUtil.ILOOP.ON);
                dao.setImageLoop(SettingUtil.ILOOP.ON);
                if(listener != null){
                    listener.onUpdateImageLoop(SettingUtil.ILOOP.ON);
                }
                break;
            case R.id.btn_image_loop_off:
                updateLoopSetting(SettingUtil.ILOOP.OFF);
                dao.setImageLoop(SettingUtil.ILOOP.OFF);
                if(listener != null){
                    listener.onUpdateImageLoop(SettingUtil.ILOOP.OFF);
                }
                break;
            default:
                break;
        }
    }
    private void updateTimerText(SettingUtil.ILOOPTIMER timer){
        if(timer.ordinal() < SettingUtil.ILOOPTIMER.MIN_1.ordinal()){
            tvTimer.setText(R.string.image_player_setting_timer_sec);
        }else{
            tvTimer.setText(R.string.image_player_setting_timer_min);
        }
    }
    private void updateLoopSetting(SettingUtil.ILOOP loop){
        if(loop == SettingUtil.ILOOP.ON){
            btnLoopOn.setSelected(true);
            btnLoopOff.setSelected(false);
            comboView.setEnabled(true);
        }else{
            btnLoopOn.setSelected(false);
            btnLoopOff.setSelected(true);
            comboView.setEnabled(false);
        }
    }
}
